/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.section;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.unis.common.util.TemplateUtil;
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.section.Section;

/**
 * @author Administrator (email:granzy@sohu.com)
 * @since 2013-02-25
 */
@Service("sectionService")
public class SectionServiceImpl implements SectionService {

    private IDataSet4iBatis iDataSet4iBatis;

    /****************************************
     * Insert a instace of Section
     ****************************************/
    public void insert(Section section) throws Exception {
        section.setSectionid(TemplateUtil.genUUID());
        iDataSet4iBatis.insert(section);
    }

    /****************************************
     * Update a instace of Section
     ****************************************/
    public void update(Section section) throws Exception {
        iDataSet4iBatis.update(section);
    }

    /****************************************
     * Delete a instace of Section
     ****************************************/
    public void delete(Section section) throws Exception {
        iDataSet4iBatis.delete(section);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(Section section) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(Section section) throws Exception {
        String statementId = section.getStatmentId();
        String mainId = section.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> sectionList = new ArrayList<BaseModel>();
        for (String id : ids) {
            Section model = new Section();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            sectionList.add(model);
        }
        iDataSet4iBatis.batchDelete(sectionList);
    }

    /****************************************
     * Query instaces of Section
     ****************************************/
    public List<BaseModel> queryAsList(Section section) throws Exception {
        String queryListStatement = "";
        if (section.isPaging()) {
            queryListStatement = section.getStatmentId();
            section.setStatmentId("Section.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(section);
            section.setTotalCount(baseModel.getTotalCount());
            section.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(section);
    }

    /****************************************
     * Query a instaces of Section
     ****************************************/
    public BaseModel queryAsObject(Section section) throws Exception {
        return iDataSet4iBatis.queryAsObject(section);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
}
